﻿using QuanLyTiemVang.BUS;
using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for ChiTietPhieuChi.xaml
    /// </summary>
    public partial class ChiTietPhieuChi : Window, IPhanQuyenHelper
    {
        public ChiTietPhieuChi()
        {
            InitializeComponent();
            this.Loaded += ChiTietPhieuChi_Loaded;
        }

        void ChiTietPhieuChi_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbMaKhoanPhi.ItemsSource = KhoanPhiDAO.GetAllKhoanPhi();
            this.dpCurrentDay.SelectedDate = DateTime.Now.Date;
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }

        private void Preview_Text_Input(object sender, TextCompositionEventArgs e)
        {
            int result;
            if (!(int.TryParse(e.Text, out result)))
            {
                e.Handled = true;
            }
        }

        private void cbMaKhoanPhi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            KHOANPHI k=cbMaKhoanPhi.SelectedItem as KHOANPHI;
            this.txtTenKhoanPhi.Text = k.TenKhoanPhi;
        }

        #region IPhanQuyenImplement
        public void SetAdminFunction()
        {
            
        }

        public void SetStaffFunction()
        {
            
        }

        public void SetCustomerFunction()
        {
            
        }

        public void SetNonFunction()
        {
            this.Close();
        }
        #endregion

        private void btnLapPhieu_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.listCTPC.Count == 0)
            {
                MessageBox.Show("Nhập các khoản chi trước!");
                return;
            }
            // update DANH_SACH_KHOAN_CHI
            int maDSKC=-1;
            DANHSACHKHOANCHI ds = DanhSachKhoanChiDAO.GetDanhSachKhoanChi(DateTime.Now.Date);
            if (ds != null)
            {
                maDSKC=ds.MaDanhSachKhoanChi;
                foreach (var item in this.listCTPC)
                {
                    ds.TienChiPhieuChi += item.ThanhTien;
                    ds.TongTienChi += item.ThanhTien;                    
                }
                DanhSachKhoanChiDAO.UpdateDanhSachKhoangChi(ds);
            }
            else
            {
                DANHSACHKHOANCHI ds2=new DANHSACHKHOANCHI();
                ds2.NgayChi = DateTime.Now.Date;
                ds2.TienChiPhieuChi = 0;
                ds2.TienChiPhieuHangGiaCong = 0;
                ds2.TienChiPhieuMuaHang = 0;
                ds2.TongTienChi = 0;

                foreach (var item in this.listCTPC)
                {
                    ds2.TienChiPhieuChi += item.ThanhTien;
                    ds2.TongTienChi += item.ThanhTien;
                }
                DanhSachKhoanChiDAO.Insert(ds2);
                maDSKC = ds2.MaDanhSachKhoanChi;
                    //DanhSachKhoanChiDAO.GetDanhSachKhoanChi(DateTime.Now).MaDanhSachKhoanChi;
            }

            // CREATE PHIEU_CHI
            //Da ton tai phieu chi trong csdl
            PHIEUCHI pc = PhieuChiDAO.getPhieuChi(DateTime.Now.Date);
            if (pc != null)
            {
                pc.TongCong += decimal.Parse(txtTongCong.Text);
                PhieuChiDAO.Update(pc);
                List<CHITIETPHIEUCHI> lstTemp = ChiTietPhieuChiDAO.GetListChiTietPhieuChi(pc.MaPhieuChi);
                for (int i = 0; i < lstTemp.Count; i++)
                {
                    for (int j = 0; j < listCTPC.Count; j++)
                    {
                        if (listCTPC[j].MaKhoanPhi == lstTemp[i].MaKhoanPhi)
                        {
                            lstTemp[i].ThanhTien += listCTPC[j].ThanhTien;
                            listCTPC.RemoveAt(j);
                        }
                    }
                }
                for (int i = 0; i < listCTPC.Count; i++)
                {
                    listCTPC[i].MaPhieuChi = pc.MaPhieuChi;
                    ChiTietPhieuChiDAO.Insert(listCTPC[i]);
                }
                for (int i = 0; i < lstTemp.Count; i++)
                {
                    ChiTietPhieuChiDAO.Update(lstTemp[i]);
                }

            }
            else
            {
                pc = new PHIEUCHI();
                pc.MaDanhSachKhoanChi = maDSKC;
                pc.NgayChi = DateTime.Now.Date;
                pc.TongCong = 0;
                for (int i = 0; i < listCTPC.Count; i++)
                    pc.TongCong += listCTPC[i].ThanhTien;

                int maPhieuChi = PhieuChiDAO.Insert(pc);

                for (int i = 0; i < listCTPC.Count; i++)
                {
                    listCTPC[i].MaPhieuChi = maPhieuChi;
                    ChiTietPhieuChiDAO.Insert(listCTPC[i]);
                }
            }

            MessageBoxResult result = MessageBox.Show("Lập phiếu thành công. Bạn có muốn in phiếu hay không?", "Thông báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.No)
            {
                return;
            }

            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
            sfd.Filter = "PDF File|*.pdf";
            sfd.FileName = "PC_" + pc.MaPhieuChi.ToString();
            System.Windows.Forms.DialogResult r = sfd.ShowDialog();
            if (r == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }

            ReportBUS.PhieuChiReport(pc.MaPhieuChi, sfd.FileName);
            MessageBox.Show("Đã in phiếu thành công.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);

            this.Reset();
        }

        private void Reset()
        {
            this.dpCurrentDay.SelectedDate = DateTime.Now.Date;
            this.txtChiPhi.Clear();
            this.txtTenKhoanPhi.Text = "";
            this.txtTongCong.Clear();
            this.listCTPC.Clear();
            this.dgDanhSachKhoanPhi.ItemsSource = null;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private List<CHITIETPHIEUCHI> listCTPC = new List<CHITIETPHIEUCHI>();

        private void btnThem_Click_1(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtChiPhi.Text))
            {
                MessageBox.Show("Nhập chi phí (vnđ)!");
                this.txtChiPhi.Focus();
                return;
            }

            // add more data for datagridview
            decimal thanhTien = decimal.Parse(this.txtChiPhi.Text);            
            KHOANPHI kp = this.cbMaKhoanPhi.SelectedItem as KHOANPHI;
            
            bool isDuplicated = false;
            for (int i = 0; i < listCTPC.Count; i++)
            {
                if (listCTPC[i].MaKhoanPhi == kp.MaKhoanPhi)
                {
                    isDuplicated = true;
                    listCTPC[i].ThanhTien += thanhTien;
                    break;
                }
            }

            if (isDuplicated == false)
            {
                CHITIETPHIEUCHI ct = new CHITIETPHIEUCHI();
                ct.ThanhTien = thanhTien;
                ct.TenKhoanPhi = kp.TenKhoanPhi;
                ct.MaKhoanPhi = kp.MaKhoanPhi;
                listCTPC.Add(ct);
            }

            decimal sum = 0;
            foreach (var item in listCTPC)
            {
                sum += item.ThanhTien;
            }
            this.txtTongCong.Text = sum.ToString();

            this.dgDanhSachKhoanPhi.ItemsSource = null;
            this.dgDanhSachKhoanPhi.ItemsSource = listCTPC;

            this.txtChiPhi.Clear();
        }

        private void dgDanhSachKhoanPhi_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                var dataGrid = (DataGrid)sender;
                for (int i = 0; i < listCTPC.Count; i++)
                {
                    listCTPC.Remove((CHITIETPHIEUCHI)dataGrid.SelectedItem);
                }

                dgDanhSachKhoanPhi.ItemsSource = null;
                dgDanhSachKhoanPhi.ItemsSource = listCTPC;

                decimal tongTien = 0;
                foreach (CHITIETPHIEUCHI item in listCTPC)
                {
                    tongTien += item.ThanhTien;
                }

                txtTongCong.Text = tongTien.ToString();
            }
        }
    }
}
